using System;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;
using BaseClass;

namespace WindowsApplication1
{
    public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            StartSendFile("localhost", 8000, @"D:\Program Files\Sina\RssReader\rssreader.exe");
        }

        private void StartSendFile(string strHost, int iPort, string strFile)
        {
            ExeCuteFile sfFile = new ExeCuteFile(strHost, iPort, strFile, "");

            StartThread(new ThreadStart(sfFile.Send));
        }

        private void StartThread(ThreadStart target)
        {
            Thread doStep = new Thread(target);
            doStep.IsBackground = true;
            doStep.Start();
        }

        public class ExeCuteFile
        {
            private string Host;
            private int Port;
            private string FileName;
            private string cmdParam;

            public ExeCuteFile(string strHost, int iPort, string strFileName, string strCmdParam)
            {
                Host = strHost;
                Port = iPort;
                FileName = strFileName;
                cmdParam = strCmdParam;
            }

            public void Send()
            {
                Socket s = null;
                try
                {
                    s = CommunClass.ConnectToServer(Host, Port);

                    CommunClass.WriteCommandToSocket(s, "EXECUTEFILE");
                    CommunClass.WriteCommandDescToSocket(s, FileName);
                    CommunClass.WriteDynamicTextToSocket(s, "", null);
                    MessageBox.Show(CommunClass.ReadDynamicTextFromSocket(s));
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
                finally
                {
                    if (s != null)
                        s.Close();
                }
            }
        }
    }
}